<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

tokenKey:<input id="tokenKey" type="text">
tokenId:<input id="tokenId" type="text">
userId:<input id="userId" type="text">
<button id="xx" onclick="test()">生成token</button>
<div id="token"></div>
</body>
<script src="sjcl.min.js"></script>
<script>
    function test() {
        var tokenKey = document.getElementById("tokenKey").value;
        var tokenId = document.getElementById("tokenId").value;
        var userId = document.getElementById("userId").value;

        //生成 token 示例
        var hmac = new sjcl.misc.hmac(tokenKey)//服务器返回的tokenKey;
        //uid :当前用户id
        //tid :服务器返回的tokenId
        //rid :随机字符串 (不能重复,可以考虑本机 ip + 当前时间戳 + 随机字符串)
        //ts : 当前时间戳
        //hash:生成签名
        var time=(new Date()).getTime();

        var a=RndNum(5);
        var param = "uid=" + userId + "&tid=" + tokenId + "&rid=" +  a+ "&ts=" + time;
        var tokens = param + "&hash=" + sjcl.codec.base64.fromBits(hmac.encrypt("111"));
        var testdiv = document.getElementById("token");
        testdiv.innerHTML = "<p>" + tokens + "</p>";
    }

    function RndNum(n) {
        var rnd = "";
        for (var i = 0; i < n; i++)
            rnd += Math.floor(Math.random() * 10);
        return rnd;
    }
</script>
</html>
